package dao

import (
	"team-evaluate/internal/model"
	"team-evaluate/pkg/app"
	"time"
)

func (d *Dao) CreateUserTeam(userId, teamId uint32, userMsg string) error {
	userTeam := model.UserTeam{
		UserId:  userId,
		TeamId:  teamId,
		UserMsg: userMsg,
	}
	return d.engine.Create(&userTeam).Error
}

func (d *Dao) CountUserTeamUser(teamId uint32) (int64, error) {
	var count int64
	if err := d.engine.Model(&model.UserTeam{}).Where("is_del = ? AND team_id = ?", 0, teamId).Count(&count).Error; err != nil {
		return 0, err
	}
	return count, nil
}

func (d *Dao) CountUserTeamTeam(userId uint32) (int64, error) {
	var count int64
	if err := d.engine.Model(&model.UserTeam{}).Where("is_del = ? AND user_id = ?", 0, userId).Count(&count).Error; err != nil {
		return 0, err
	}
	return count, nil
}

func (d *Dao) CountUTTeamByTN(userId uint32, userName string) (int64, error) {
	var count int64
	if err := d.engine.
		Table("user_team").
		Joins("left join team on user_team.team_id = team.id").
		Where("user_team.is_del = ? AND user_id = ? AND team.name = ?", 0, userId, userName).
		Count(&count).Error; err != nil {
		return 0, err
	}
	return count, nil
}

func (d *Dao) GetUTTeamListByTN(userId uint32, teamName string, page, pageSize int) ([]*model.UserTeamTeamListResponse, error) {
	pageOffset := app.GetPageOffset(page, pageSize)
	var utTeamList []*model.UserTeamTeamListResponse
	var err error
	if pageOffset >= 0 && pageSize > 0 {
		d.engine = d.engine.Offset(pageOffset).Limit(pageSize)
	}
	err = d.engine.Table("user_team").
		Joins("left join team on user_team.team_id = team.id").
		Where("user_team.is_del = ? AND user_id = ? AND team.name = ? AND team.is_del = false", 0, userId, teamName).
		Scan(&utTeamList).Error
	if err != nil {
		return nil, err
	}
	return utTeamList, nil
}

func (d *Dao) GetUserTeamUserList(teamId uint32, page, pageSize int) ([]*model.UserTeamUserListResponse, error) {
	pageOffset := app.GetPageOffset(page, pageSize)
	var utUserList []*model.UserTeamUserListResponse
	var err error
	if pageOffset >= 0 && pageSize > 0 {
		d.engine = d.engine.Offset(pageOffset).Limit(pageSize)
	}
	err = d.engine.Table("user_team").
		Select("user_team.id, user_team.created_on, user_team.modified_on, user_team.is_active, user_team.tag, user_team.user_id, user.name, user_team.user_msg").
		Joins("left join user on user_team.user_id = user.id").
		Where("user_team.is_del = ? AND user_team.team_id = ?", 0, teamId).
		Scan(&utUserList).Error
	if err != nil {
		return nil, err
	}
	return utUserList, nil
}

func (d *Dao) GetUserTeamTeamList(userId uint32, page, pageSize int) ([]*model.UserTeamTeamListResponse, error) {
	pageOffset := app.GetPageOffset(page, pageSize)
	var utTeamList []*model.UserTeamTeamListResponse
	var err error
	if pageOffset >= 0 && pageSize > 0 {
		d.engine = d.engine.Offset(pageOffset).Limit(pageSize)
	}
	err = d.engine.Table("user_team").
		Joins("left join team on user_team.team_id = team.id").
		Where("user_team.is_del = ? AND user_team.user_id = ? AND team.is_del = false", 0, userId).
		Scan(&utTeamList).Error
	if err != nil {
		return nil, err
	}
	return utTeamList, nil
}

func (d *Dao) FindUserTeamById(id uint32) (*model.UserTeam, error) {
	userTeam := model.UserTeam{}
	err := d.engine.Table("user_team").Where("is_del = ? AND id = ?", 0, id).Scan(&userTeam).Error
	if err != nil {
		return nil, err
	}
	return &userTeam, nil
}

func (d *Dao) UserTeamAgree(ID uint32) error {
	return d.engine.Model(&model.UserTeam{}).
		Where("id = ? AND is_del = ?", ID, 0).
		Update("is_active", true).Error
}

func (d *Dao) DeleteUserTeam(id uint32) error {
	nowTime := time.Now().Unix() * 1000
	return d.engine.Exec("update user_team set is_del = ?, deleted_on = ? where id = ?", true, nowTime, id).Error
}

func (d *Dao) FindUserTeamByUT(userId uint32, teamId uint32) (*model.UserTeam, error) {
	userTeam := model.UserTeam{}
	err := d.engine.Table("user_team").Where("is_del = ? AND user_id = ? AND team_id = ?", 0, userId, teamId).Scan(&userTeam).Error
	if err != nil {
		return nil, err
	}
	return &userTeam, nil
}
